import pytest
import os.path
import requests
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil

class TestRepoManagement:
    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/GET_get_all_helm_repo.yaml'))
    def test_get_all_helm_repo(self, yaml, get_env_info):
        # 查询所有Helm仓库信息
        baseUrl = get_env_info
        url = baseUrl +yaml['request']['url']
        method = yaml['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/GET_get_specified_helm_repo.yaml'))
    def test_get_specified_helm_repo(self, yaml, get_env_info):
        # 查询指定Helm仓库信息
        baseUrl = get_env_info
        url = baseUrl + yaml['request']['url']
        method = yaml['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/POST_add_helm_repo.yaml'))
    def test_add_helm_repo(self, yaml, get_env_info):
        # 添加Helm仓库
        baseUrl = get_env_info
        url = baseUrl + yaml['request']['url']
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data =yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/PUT_modify_helm_repo.yaml'))
    def test_modify_helm_repo(self, yaml, get_env_info):
        # 修改Helm仓库
        baseUrl = get_env_info
        url = baseUrl + yaml['request']['url']
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        data = yaml['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/DELETE_delete_helm_repo.yaml'))
    def test_delete_helm_repo(self, yaml, get_env_info):
        # 删除Helm仓库
        baseUrl = get_env_info
        url = baseUrl + yaml['request']['url']
        method = yaml['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/POST_sync_all_helm_repo.yaml'))
    def test_sync_all_helm_repo(self, yaml, get_env_info):
        # 同步全部Helm仓库
        baseUrl = get_env_info
        url = baseUrl + yaml['request']['url']
        method = yaml['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/POST_sync_specified_helm_repo.yaml'))
    def test_sync_specified_helm_repo(self, yaml, get_env_info):
        # 同步指定Helm仓库
        baseUrl = get_env_info
        url = baseUrl + yaml['request']['url']
        method = yaml['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/GET_get_helm_repo_sync_status.yaml'))
    def test_get_helm_repo_sync_status(self, yaml, get_env_info):
        # 获取仓库同步状态
        baseUrl = get_env_info
        url = baseUrl + yaml['request']['url']
        method = yaml['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/GET_get_charts.yaml'))
    def test_get_charts(self, yaml, get_env_info):
        # 查询仓库中的Chart列表（最新）
        baseUrl = get_env_info
        url = baseUrl + yaml['request']['url']
        method = yaml['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/POST_upload_chart_to_localrepo.yaml'))
    def test_upload_chart_to_locorepo(self, yaml, get_env_info):
        # 上传Helm Chart
        baseUrl = get_env_info
        url = baseUrl + yaml['request']['url']
        method = yaml['request']['method']
        headers = yaml['request']['headers']
        # filePath = os.path.dirname(__file__) + '/../../../data/v24.06/repoManagement/stress-runner-ext-long.tgz'
        filePath = "C:\\Users\\Administrator\\PycharmProjects\\test-cases\\data\\v24.06\\repoManagement\\stress-runner-ext-long.tgz"
        # with open(filePath, 'rb') as files:
        #     req = RequestUtil().send_request(method, url, files=files)
        files = {'file': open(filePath, 'rb')}
        req = RequestUtil().send_request(method, url, files=files)
        files['file'].close()
        print(req.content)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/DELETE_delete_chart_all_version.yaml'))
    def test_delete_chart_all_version(self, yaml, get_env_info):
        # 删除Helm Chart（全部版本）
        baseUrl = get_env_info
        url = baseUrl + yaml['request']['url']
        method = yaml['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('yaml', YamlUtil().read_testcase_yaml('v24.06/repoManagement/DELETE_delete_chart_specified_version.yaml'))
    def test_delete_chart_specified_version(self, yaml, get_env_info):
        # 删除Helm Chart（指定版本）
        baseUrl = get_env_info
        url = baseUrl + yaml['request']['url']
        method = yaml['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == yaml['validate'][0]['equals']['status_code']